package com.ruimei.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruimei.entitys.Evaluate;
import com.ruimei.entitys.Parent;
import com.ruimei.entitys.ParentComment;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.EvaluateService;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@Api(tags={"教师评语"})
public class EvaluateConttoller {

    @Resource
    EvaluateService evaluateService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;


    @ApiOperation("通过教师ID查询")
    @PostMapping({"/evaluate"})
    @ResponseBody
    public Map<String, Object> getEvaluate(String classId,String studentName,String evaluateId,HttpServletRequest request)
    {

        Map<String, Object> map = evaluateService.getEvaluate(classId,studentName,evaluateId,TokenUtils.getTeacherId(request));
        return map;
    }

    @ApiOperation("教师端添加教师评语")
    @PostMapping({"/evaluate/add"})
    @ResponseBody
    public Map<String, Object> addEvaluate(Evaluate evaluate, HttpServletRequest request)
    {

        Teacher teacher = new Teacher();
        teacher.setId(TokenUtils.getTeacherId(request));
        if(redisTemplate.hasKey(teacher.getId())){
            teacher = JSONArray.parseObject(redisTemplate.opsForValue().get(teacher.getId()),Teacher.class);
            evaluate.setTeacherId(teacher.getId());
            evaluate.setTeacherName(teacher.getFullName());
            Map<String, Object> map = evaluateService.addEvaluate(evaluate);
            return map;
        }


        return null;
    }

    @ApiOperation("修改教师评语")
    @PostMapping({"/evaluate/set"})
    @ResponseBody
    public Map<String, Object> setEvaluateT(Evaluate evaluate, HttpServletRequest request)
    {
        String id = TokenUtils.getTeacherId(request);
        Map<String, Object> map = evaluateService.setEvaluateT(evaluate,id);
        return map;
    }

    @ApiOperation("家长端通过学生ID查询")
    @PostMapping({"/parent/evaluate"})
    @ResponseBody
    public Map<String, Object> getEvaluate(String studentId,String evaluateId)
    {
        Map<String, Object> map = evaluateService.getEvaluateP(studentId,evaluateId);
        return map;
    }

    @ApiOperation("获取家长端回复")
    @PostMapping({"/parent/getParentComment"})
    @ResponseBody
    public Map<String, Object> getParentComment(String title,String studentId)
    {

        Map<String, Object> map = evaluateService.getParentComment(title,studentId);
        return map;
    }

    @ApiOperation("家长端回复")
    @PostMapping({"/parent/setEvaluate"})
    @ResponseBody
    public Map<String, Object> setParentComment(ParentComment parent)
    {

        Map<String, Object> map = evaluateService.setParentComment(parent);
        return map;
    }

    @ApiOperation("教师删除评语")
    @PostMapping({"/deleteEvaluate"})
    @ResponseBody
    public Map<String, Object> deleteEvaluate(String evaluateId)
    {

        Map<String, Object> map = evaluateService.deleteEvaluate(evaluateId);
        return map;
    }


}
